/*
 * @Author: yxfan
 * @Date: 2025-03-01 14:22:00
 * @LastEditTime: 2025-03-01 14:22:07
 * @FilePath: \umi-demo\src\components\hooks\useOffsets.ts
 * @Description:
 */
import { useEffect, useState } from 'react';
import { TabItem, TabSizeMap, TabOffsetMap } from '../interface';

/**
 * 计算标签页的偏移量
 * @param tabs 标签页数组
 * @param tabSizes 标签页尺寸信息
 * @param containerWidth 容器宽度
 * @returns 标签页偏移量Map
 */
export default function useOffsets(
    tabs: TabItem[],
    tabSizes: TabSizeMap,
    containerWidth: number,
): TabOffsetMap {
    const [offsets, setOffsets] = useState<TabOffsetMap>(new Map());

    useEffect(() => {
        const map: TabOffsetMap = new Map();

        // 计算每个标签页的偏移量
        tabs.forEach(({ key }) => {
            const data = tabSizes.get(key) || {
                width: 0,
                height: 0,
                left: 0,
                top: 0,
            };
            const entity = {
                ...data,
                right: containerWidth - data.left - data.width,
            };
            map.set(key, entity);
        });

        setOffsets(map);
    }, [tabs, tabSizes, containerWidth]);

    return offsets;
}
